package com.kz.himalaya.util;

import android.content.Context;
import android.databinding.BindingAdapter;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.kz.himalaya.BR;
import com.kz.himalaya.bean.DiscoveryEntity;
import com.squareup.picasso.Picasso;

/**
 * Created by Administrator on 2017/1/28.
 */

public class Binding {
    //图片加载
    @BindingAdapter("loadPic")
    public static void loadPic(ImageView imageView, String url) {
        Picasso.with(imageView.getContext())
                .load(url)
                .into(imageView);
    }
    //图片加载(圆形图片裁剪)
    @BindingAdapter("loadCirclePic")
    public static void loadCirclePic(ImageView view,String url){
        Picasso.with(view.getContext())
                .load(url)
                .transform(new CircleTransformation())
                .into(view);
    }
    //首页热门焦点图下的discovery
    @BindingAdapter({"loadDiscoveryItemData","discoveryItemLayoutId"})
    public static void loadDiscoveryItem(LinearLayout layout, DiscoveryEntity data, int layoutId) {
        layout.removeAllViews();
        Context context = layout.getContext();
        for (int i = 0; i < data.getList().size(); i++) {
            ViewDataBinding binding = DataBindingUtil.inflate(LayoutInflater.from(context), layoutId, layout, false);
            View view = binding.getRoot();
            if (i!=0) {
                LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) view.getLayoutParams();
                params.leftMargin = 50;
                view.setLayoutParams(params);
            }
            binding.setVariable(BR.discoveryItem,data.getList().get(i));
            layout.addView(view);
        }
    }
    //播放时间格式化
    @BindingAdapter("timeFormat")
    public static void timeFormat(TextView view,int duration){
        int mm = duration / 60;
        int ss = duration % 60;
        view.setText(""+mm+":"+ss);
    }
}
